<?php
/* Created by: Ignas Janickas
 * Date: 2011-12-07
 * Class works with users
 */
class User {
    private $link;
    public $id;
    public $data;
    public $feedback = array();
    public $works = array();
    public $asks = array();
    
    /*
     * Constructor gets data linked with user id
     * @param - user id
     * @ptype - integer
     * 
     * Constructor creates new entry in db and gets data about it
     * @param1 - user name
     * @ptype1 - string
     * @param2 - user surname
     * @ptype2 - string
     * @param3 - user email
     * @ptype3 - string
     */
    function __construct($idn, $surn = null, $email = null) {
        require_once("db.class.php");
        include("mysql.config.php");
        
        //echo $GLOBALS['db_address'] . " oho " . $GLOBALS['db_username'];
        $this->link = new DataBase($db_address, $db_username, $db_password, $db_db);
                
        if (($surn != null) and ($email != null)) {
            $this->link->insert("users", null, null);
            $this->id = mysql_insert_id();
            $this->setName($idn);
            $this->setSurname($surn);
            $this->setEmail($email);
        } else {
            $this->id = $idn;
        }//if $surn and $email <> null
        
        $this->update();
    }//construct
    
    /*
     * Function updates $data param
     */
    private function update() {
        $this->link->select("users","*","id='$this->id'");
        $this->data = $this->link->getResult();
    }//update
    
    /*
     * Function gets user's email
     * @return - returns user's email
     * @rtype - string
     */
    function getEmail() {
        return $this->data['email'];
    }//getEmail
    
    /*
     * Function gets user's name
     * @return - returns user's name
     * @rtype - string
     */
    function getName() {
        return $this->data['name'];
    }//getName
    
    /*
     * Function gets user's surname
     * @return - returns user's surname
     * @rtype - date / string
     */
    function getSurname() {
        return $this->data['surname'];
    }//getSurname
    
    /*
     * Function gets user's id (set from LDAP)
     * @return - returns user's id
     * @rtype - date / string
     */
    function getId() {
        return $this->data['id'];
    }//getId
    
    public function getWorks() {
        $tmp_db = new DataBase("localhost","root","asdasd","hiveme");
        $tmp_db->select("`works`","`id`","`uid`='$this->id'");
        $data = $tmp_db->getResult();
        $return = array();
        foreach ($data as $work) {
            foreach ($work as $wid) {
                $return[] = $wid;
            }
        }
        unset ($tmp_db);
        return $return;
    }
    
    public function getFeedbacks() {
        $tmp_db = new DataBase("localhost","root","asdasd","hiveme");
        $tmp_db->select("`works`","`id`","`uid`='$this->id'");
        $data = $tmp_db->getResult();
        $return = array();
        foreach ($data as $work) {
            foreach ($work as $wid) {
                $return[] = $wid;
            }
        }
        unset ($tmp_db);
        return $return;
    }
    
    public function getAsks() {
        $tmp_db = new DataBase("localhost","root","asdasd","hiveme");
        $query = $tmp_db->query("SELECT `a`.`id` FROM `asks` AS `a` INNER JOIN `works` AS `w` ON `a`.`workid`=`w`.`id` AND `w`.`uid` = '$this->id'")
                or die(mysql_error());
        $return = array();
        while ($ask = $tmp_db->fetch($query)) {
            $return[] = $ask->id;
        }
        unset ($tmp_db);
        return $return;
    }
    
    /*
     * Function sets user's name
     * @param - name
     * @ptype - string
     * @return - 1 if set, 0 if fail
     * @rtype - boolean
     */
    function setName($name) {
        $res = $this->link->query("UPDATE `users` SET `name` = '$name' WHERE `id` = '$this->id'");
        if ($res) {
            $this->update();
            return 1;
        } else
            return 0;
    }//setName
    
    /*
     * Function sets user's surname
     * @param - surname
     * @ptype - string
     * @return - 1 if set, 0 if fail
     * @rtype - boolean
     */
    function setSurname($surname) {
        $res = $this->link->query("UPDATE `users` SET `surname` = '$surname' WHERE `id` = '$this->id'");
        if ($res) {
            $this->update();
            return 1;
        } else
            return 0;
    }//setSurname
    
    /*
     * Function sets user's email
     * @param - email
     * @ptype - string
     * @return - 1 if set, 0 if fail
     * @rtype - boolean
     */
    function setEmail($email) {
        $res = $this->link->query("UPDATE `users` SET `email` = '$email' WHERE `id` = '$this->id'");
        if ($res) {
            $this->update();
            return 1;
        } else
            return 0;
    }//setEmail
}//class User
?>
